
package com.catsic.core.specification;

import javax.persistence.criteria.Path;

import com.catsic.base.specification.BaseSpec;
import com.catsic.base.specification.QueryWraper;
import com.catsic.core.conditon.UserCondition;
import com.catsic.core.entity.Depart;
import com.catsic.core.entity.Organ;
import com.catsic.core.entity.User;
/**
 * ClassName: UserSpec
 * Description: UserSpec
 * date: 2013年4月28日 下午2:30:29
 * author: yangyd
 */
public class UserSpec extends BaseSpec<User> {

	public UserSpec(UserCondition condition){
		super(condition);
	}
	
	/**
	 * @see com.catsic.core.specification.BaseSpec#addCondition(com.catsic.core.specification.QueryWraper)
	 */
	@Override
	protected void addCondition(QueryWraper<User> queryWraper) {
		UserCondition condition = (UserCondition) getCondition();
		addLikeCondition(queryWraper, "username");
		addLikeCondition(queryWraper, "realname");
		/**
		 * 多表联查,多对一,机构
		 */
		Path<Organ> organ = queryWraper.getRoot().get("organ");
		addEqualCondition(queryWraper, organ.get("id"),condition.getOrganId());
		//addLikeCondition(queryWraper, organ.get("name"),"admin");
		
		/**
		 * 多表联查,多对一,部门
		 */
		Path<Depart> depart = queryWraper.getRoot().get("depart");
		addLikeCondition(queryWraper, depart.get("name"),condition.getDepartName());
		/**
		 * 多表联查,多对多,
		 */
		
		/*if(condition.getRoleName() != null){
            Join<User,List<Role>> roleJoin = queryWraper.getRoot().join("roles")
			addEqualCondition(queryWraper, roleJoin.get("name"), condition.getRoleName());
		}*/
		
		/**
		 * 自定义排序
		 */
		//queryWraper.addOrder(queryWraper.getCb().desc(queryWraper.getRoot().get("organ").get("id")));
	    
		/**
		 * 时间段查询
		 */
		/*Path<Date> dealTime = queryWraper.getRoot().get("dealTime");
		if(condition.getStartDate() != null){
			queryWraper.addPredicate(queryWraper.getCb().greaterThanOrEqualTo(dealTime, condition.getStartDate()));
		}
		if(condition.getEndDate() != null){
			queryWraper.addPredicate(queryWraper.getCb().lessThanOrEqualTo(dealTime, condition.getEndDate()));
		}*/
	}
}
